package com.xiaofei.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

public class IoOperation {
	InputStream inputStream;
	OutputStream outputStream;
	@Before
	public void creatInputStream(){
		try {
			 inputStream = new FileInputStream("C:/Users/user/Desktop/linux命令总结.txt");
			 outputStream= new FileOutputStream("C:/Users/user/Desktop/aa.txt");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void readWithFileInputStream() throws IOException{
		long currentTime=System.currentTimeMillis();
		System.out.println("当前时间："+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(currentTime)));
		int lenth =0;
		byte[] b= new byte[1024];
		while((lenth=inputStream.read(b))!=-1){
			System.out.println(new String(b,0,lenth,"gbk"));
		}
		long endTime =System.currentTimeMillis();
		
		System.out.println("总共用了"+(endTime-currentTime)+"毫秒");
		
	}
	
	@Test
	public void writeWithFileOutStream() throws IOException{
		long currentTime=System.currentTimeMillis();
		System.out.println("当前时间："+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(currentTime)));
		int lenth =0;
		byte[] b= new byte[1024];
		while((lenth=inputStream.read(b))!=-1){
			//System.out.println(new String(b,0,lenth,"gbk"));
			outputStream.write(b, 0, lenth);
		}
		long endTime =System.currentTimeMillis();
		
		System.out.println("总共用了"+(endTime-currentTime)+"毫秒");
		outputStream.close();//后开先关
		inputStream.close();
	}
}
